package com.czk.zookeeper.zookeepercli.config;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/5 20:48
 */
public class MyConfigCenter implements Watcher {
    /**
     * 计数器对象
     */
    static CountDownLatch countDownLatch = new CountDownLatch(1);
    /**
     * 连接对象
     */
    static ZooKeeper zooKeeper;

    /**
     * 声明数据库连接参数
     */
    private String url;
    private String username;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 读取服务器配置信息
     */
    public void read() {
        try {
            // 创建连接对象
            zooKeeper = new ZooKeeper("192.168.25.133:2181", 10000, this);
            // 由于是异步，所以是需要等待
            countDownLatch.await();
            System.out.println("=======");
            // 读取配置信息
            this.url = new String(zooKeeper.getData("/config/url", true, null));
            this.username = new String(zooKeeper.getData("/config/username", true, null));
            this.password = new String(zooKeeper.getData("/config/password", true, null));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            MyConfigCenter configCenter = new MyConfigCenter();
            // 循环
            for (int i = 0; i <= 10; i++) {
                TimeUnit.SECONDS.sleep(10);
                System.out.println("url=" + configCenter.getUrl());
                System.out.println("username=" + configCenter.getUsername());
                System.out.println("password=" + configCenter.getPassword());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public MyConfigCenter() {
        // 初始化
        read();
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        try {
            // 捕获事件类型
            if (watchedEvent.getType() == Event.EventType.None) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    //
                    System.out.println("连接成功");
                    // 计数器减一
                    countDownLatch.countDown();
                }
                if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
                    System.out.println("断开连接");
                }
                if (watchedEvent.getState() == Event.KeeperState.Expired) {
                    System.out.println("会话超时");
                }
                if (watchedEvent.getState() == Event.KeeperState.AuthFailed) {
                    System.out.println("认证失败");
                }
                if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                    // 捕获到配置信息节点发生变化时重新获取
                    System.out.println("节点发生变化");
                    read();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

